package com.example.api.controller.admin;

import com.example.api.constant.StatusConst;
import com.example.api.dto.UserDTO;
import com.example.api.exception.ParamException;
import com.example.api.properties.JWTProperties;
import com.example.api.query.UserQuery;
import com.example.api.service.UserService;
import com.example.api.utils.JWTUtil;
import com.example.api.utils.PageResult;
import com.example.api.utils.Result;
import com.example.api.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户接口")
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Resource
    JWTProperties jwtProperties;

    @Resource
    UserService userService;


    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<List<UserVO>> login(String loginName, String password){
        //参数的校验
        if(StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)){
            throw new ParamException("账号和密码都不能为空");
        }

        UserVO userVO = userService.login(loginName,password);
        if(userVO==null){
            return Result.failure("账号或密码错误");
        }

        if(userVO.getStatus()== StatusConst.INACTIVE_STATUS){
            return Result.failure("账号被冻结");
        }

        //服务器生成token --> 响应给前端----->前端每次发请求都携带token------->服务器要校验

        Map<String,Object> payload = new HashMap<>();
        payload.put(JWTUtil.USER_ID,userVO.getId());

        String token = JWTUtil.generateToken(payload,jwtProperties.getSecure(),jwtProperties.getExpire());

        Map<String,Object> data = new HashMap<>();
        data.put("token",token);
        data.put("userInfo",userVO);

        return Result.ok("成功",data);
    }

    @ApiOperation("添加用户")
    @PostMapping
    public Result add(UserDTO userDTO){
        userService.add(userDTO);
        return Result.ok("添加成功");
    }

    @ApiOperation("编辑用户")
    @PutMapping("/{id}")
    public Result update(@PathVariable long id,UserDTO userDTO){ //@RequestBody将前端传递的json格式的请求参数转为对象
        userService.update(id,userDTO);
        return Result.ok("修改成功");
    }

    @ApiOperation("禁用启动用户")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable long id,String status){
        if(StatusConst.INACTIVE_STATUS_NAME.equals(status)){
            userService.updateStatus(id,StatusConst.INACTIVE_STATUS);
        }else if(StatusConst.ACTIVE_STATUS_NAME.equals(status)){
            userService.updateStatus(id,StatusConst.ACTIVE_STATUS);
        }else{
            throw new ParamException("参数错误");
        }
        return Result.ok("修改状态成功");
    }


    @ApiOperation("查询用户")
    @GetMapping
    public Result<List<UserVO>> find(UserQuery userQuery){ //userQuery封装了查询的条件
        PageResult<UserVO> page = userService.find(userQuery);

        return Result.ok("成功",page.getList(),page.getTotal());
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable long id){
        userService.deleteById(id);
        return Result.ok("删除成功");
    }
}
